package org.stwy.pro.asset;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stwy.comm.base.BaseRecordsController;
import org.stwy.comm.util.ExcelUtil;

import com.rap.common.exception.ServiceException;
import com.rap.common.utils.FileUtils;
import com.rap.frame.web.ActionResultModel;

/*
 * 物业 控制类
 */
/**
*版权信息 (c) RAP 保留所有权利.
*@author Rap.Codegen
*@Team RAP Team
*@Date 2015-4-4 17:05:42
*<p></p>
*
*@参与人：Rap.Codegen
*/

@Controller
@RequestMapping(value = "/pro/asset")
public class AssetController extends BaseRecordsController<AssetEntity> {

	public AssetController() {
		setResourceIdentity("stwy:pro:asset");
	}
	private AssetService getService(){
		return (AssetService)super.baseService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String view(Model model) {
		super.assertHasViewPermission();
		return "stwy/pro/asset/asset_main";
	}

	@RequestMapping(value = "/select")
	public String select(Model model, String callback) {
		model.addAttribute("callback", callback);
		return "stwy/pro/asset/asset_select";
	}
	
	@RequestMapping(value = "/nullArea")
	public String nullArea(Model model) {
		return "stwy/pro/asset/asset_nullarea";
	}
	@RequestMapping(value = "/selfuse")
	public String selfuse(Model model) {
		return "stwy/pro/asset/asset_selfuse";
	}
	@RequestMapping(value = "/canNotRent")
	public String canNotRent(Model model) {
		return "stwy/pro/asset/asset_cannotrent";
	}
	@RequestMapping(value = "/view")
	public String assetView(Model model) {
		return "stwy/pro/asset/asset_view";
	}
	
	@RequestMapping(value = "/showHistory")
	public String showHistory(Model model) {
		return "stwy/pro/asset/asset_history";
	}
	@RequestMapping(value = "/hasdeleted")
	public String hasdeleted(Model model) {
		return "stwy/pro/asset/asset_hasdeleted";
	}
	/**
	 * 添加变更记录
	 * @param record
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/addAssetRecord")
	@ResponseBody
	private ActionResultModel addAssetRecord(RecordEntity record,String[] pks) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().addAssetRecord(record,pks);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return arm;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/updateAssetFileState")
	@ResponseBody
	private ActionResultModel updateAssetFileState(String entityType,String entityUuid) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().updateAssetFileState(entityType,entityUuid);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return arm;
	}
	@RequestMapping(value = "/import")
	@ResponseBody
	public String importData(Model model) throws IOException {
		File file = new File("D:/assest.xls");
		List<List<Object>> dataList= ExcelUtil.readFirstSheet(FileUtils.openInputStream(file));
		try {
			getService().importData(dataList);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "成功";
	}
}
